/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Yongke
 */
@WebServlet(name="userlist", urlPatterns={"/userlist"})
public class userlist extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        HttpSession session = request.getSession();
        out.println("<script type='text/javascript' language='Javascript' src='home.js'></script>");
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/myrc?" + "user=progin&password=progin");
            stmt = (Statement) conn.createStatement();
            String query = "SELECT * FROM friend WHERE Username='" + session.getAttribute("username") + "'";
            if (stmt.execute(query))
            {
                rs = stmt.getResultSet();
            }
            if (rs != null)//query statement SELECT
            {
                out.println("<label>All Friend<br /></label>");
                while (rs.next())
                {
                    Statement state = null;
                    ResultSet res = null;
                    state = (Statement) conn.createStatement();
                    if (state.execute("SELECT * FROM user WHERE username='" + rs.getString("UNFriend") + "'"))
                    {
                        res = state.getResultSet();
                    }
                    if (res.next())
                    {

                        if ((res.getInt("Role") == 1))
                        {
                            //moderator
                            out.println("<a href=\"javascript:newtab('" + rs.getString("UNFriend")+ "')\" style='color: red;'>"+rs.getString("UNFriend")+"</a>");
//                            out.println("<a href=\"javascript:chatFriend('" + rs.getString("UNFriend")+ "')\" style='color: green;'>"+rs.getString("UNFriend")+"</a>");
                            out.println("<br />");
                            //out.println("<a style='color: green;' href=\"javascript:newTab('" + rs.getString("friended")
                                    //+ "')\">" + rs.getString("UNFriend") + "</a><br />");
                        }
                        else if ((res.getInt("Role") == 0))
                        {
                            //userbiasa
                            out.println("<a href=\"javascript:newtab('" + rs.getString("UNFriend")+ "')\" style='color: red;'>"+rs.getString("UNFriend")+"</a>");
                            //out.println("<a href=\"javascript:chatFriend('" + rs.getString("UNFriend")+ "')\" style='color: red;'>"+rs.getString("UNFriend")+"</a>");
                            out.println("<br />");
                            //out.println("<a href=\"javascript:newTab('" + rs.getString("friended")
                                    //+ "')\">" + rs.getString("UNFriend") + "</a><br />");
                        }
                    }
                }
            }
            conn.close();
        }
        catch (SQLException ex)
        {
            out.println("SQLException: " + ex.getMessage());
            out.println("SQLState: " + ex.getSQLState());
            out.println("VendorError: " + ex.getErrorCode());
        }
        catch (Exception ex)
        {
            out.println(ex.toString());
        }
        finally {
            if (rs != null) {
                try {
                rs.close();
                } catch (SQLException sqlEx) { } // ignore
                rs = null;
            }
            if (stmt != null) {
                try {
                stmt.close();
                } catch (SQLException sqlEx) { } // ignore
                stmt = null;
            }
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
